package com.dsshare.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;

/**
 * @title  GsonUtil.java
 * @description Java对象和JSON字符串相互转化工具类
 * @author penghuaiyi 
 * @date 2016/9/8
 * @time 15:47
 * @version 1.0
 */  
public final class GsonUtil {  
      
    private GsonUtil(){}  
      
    /** 
     * 对象转换成json字符串 
     * @param obj  
     * @return  
     */  
    public static String toJson(Object obj) {  
        Gson gson = new Gson();
        return gson.toJson(obj);  
    } 
    
    public static String toJsonForDate(Object obj) {  
    	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(obj);  
    }
  
    public static String toJsonForDateWithoutTime(Object obj) {  
    	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        return gson.toJson(obj);  
    }
    /** 
     * json字符串转成对象 
     * @param str   
     * @param type 
     * @return  
     */  
    public static <T> T fromJson(String str, Type type) {  
        Gson gson = new Gson();
        return gson.fromJson(str, type);  
    }  
  
    /** 
     * json字符串转成对象 
     * @param str   
     * @param type  
     * @return  
     */  
    public static <T> T fromJson(String str, Class<T> type) {  
        Gson gson = new Gson();
        return gson.fromJson(str, type);  
    }  
  
}  